package com.yc.swing.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.ly.demo.taach.jdbc.impl.DeptDao;
import com.yc.swing.dao.DeptDaoSub;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Vector;
import java.awt.event.ActionEvent;

public class DeptEdit extends JDialog {

	/**
	 * 自动生成序列化版本编号
	 */
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTextField textField;
	private JTextField dnameTf;
	private JTextField locTf;

	// 部门信息存放的集合
	Vector<Object> dept;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			DeptEdit dialog = new DeptEdit();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public DeptEdit(Vector<Object> dept) {
		this();
		this.dept = dept;
		this.textField.setText(dept.get(0) + "");
		this.dnameTf.setText(dept.get(1) + "");
		if (dept.get(2) != null) {
			this.locTf.setText(dept.get(2) + "");
		}
	}

	/**
	 * Create the dialog.
	 */
	public DeptEdit() {
		// 模态窗口
		setModal(true);
		// 不可以改变大小
		setResizable(false);
		setTitle("部门信息编辑");
		setBounds(100, 100, 381, 253);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		// 流式布局 20, 20
		FlowLayout fl_contentPanel = new FlowLayout(FlowLayout.CENTER, 20, 20);
		fl_contentPanel.setAlignOnBaseline(true);
		contentPanel.setLayout(fl_contentPanel);

		JLabel lblNewLabel = new JLabel("部门编号:");
		contentPanel.add(lblNewLabel);

		textField = new JTextField();
		textField.setEditable(false);
		contentPanel.add(textField);
		textField.setColumns(20);

		JLabel lblNewLabel_1 = new JLabel("部门名称");
		contentPanel.add(lblNewLabel_1);

		dnameTf = new JTextField();
		contentPanel.add(dnameTf);
		// 设置文本框宽度
		dnameTf.setColumns(20);

		JLabel lblNewLabel_2 = new JLabel("部门地址");
		contentPanel.add(lblNewLabel_2);

		locTf = new JTextField();
		contentPanel.add(locTf);
		locTf.setColumns(20);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("确定");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						// 输入部门信息完成
						// 获取用户输入的信息
						String dname = dnameTf.getText();
						String loc = locTf.getText();
						// 存储用户信息 (事务实现自动生成编号)
						DeptDao dao = new DeptDaoSub();
						try {
							if (textField.getText().isEmpty()) {
								dao.insert(dname, loc);
							} else {
								int deptno = Integer.parseInt(textField.getText());
								dao.update(deptno, dname, loc);
							}
							JOptionPane.showMessageDialog(null, "部门保存成功!");
						} catch (SQLException e1) {
							// 必须打印异常
							e1.printStackTrace();
							// 提示用户
							JOptionPane.showMessageDialog(null, "系统繁忙,请稍后再试!");
							// 发送邮件给后台管理员  ....
						}
						// 后续: 1. 自动关闭窗口,  2. 继续新部门录入
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("关闭");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						// 关闭当前窗口  , 内部类调用外部类的this的方法
						DeptEdit.this.dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		// 居中
		setLocationRelativeTo(null);
	}
}
